<template>
  <page-view>
    <el-card>
      <template #header>
        <div class="font-bold">评论列表</div>
      </template>
      <el-form :model="queryFormData" :inline="true" class="flex">
        <el-form-item label="菜品名称">
          <el-input placeholder="请输入菜品名称" v-model="queryFormData.food_name"></el-input>
        </el-form-item>

        <div class="demo-date-picker">
          <div class="block">
            <span class="demonstration mr-10">评论时间</span>
            <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" :size="size" />
          </div>
        </div>

        <el-form-item>
          <el-button type="primary" @click="queryData" :loading="isLoading" class="ml-15">
            <el-icon>
              <Search />
            </el-icon>
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <div v-loading="isLoading">
        <!--要展示数据了-->
        <el-table :data="resultData.listData" border stripe max-height="550">
          <el-table-column label="ID" prop="id" fixed="left" width="90" align="center"></el-table-column>
          <el-table-column align="center" label="菜品名称" fixed="left" prop="foodInfo.food_name" width="150">
          </el-table-column>
          <el-table-column label="评论时间" width="300" align="center">
            <template #default="{ row }">
              {{ formatDateTime(row.create_time) }}
            </template>
          </el-table-column>
          <el-table-column label="用户" prop="userInfo.nickName" width="350" align="center">
          </el-table-column>
          <el-table-column label="评论内容" align="center" width="300">
            <template #default="{ row, $index }">
              <el-link @click="DrawerMenu($index)" type="primary">
                {{ txtEllipsis(row.foodInfo.food_desc, 15) }}
                <el-icon>
                  <View />
                </el-icon>
              </el-link>
            </template>
          </el-table-column>

          <el-table-column label="星级" align="center" width="170">
            <template #default="{ row }">
              <el-rate v-model="row.star" disabled show-score text-color="#ff9900" score-template="{value}分">
              </el-rate>
            </template>
          </el-table-column>
          <el-table-column label="是否显示" align="center" width="120" fixed="right">
            <template #default="{ row }">
              <el-switch :data-id="row.id" v-model="row.isShow" :active-value="true" :inactive-value="false"
                active-color="#13ce66" inactive-color="#ff4949" @change="ShowTitle(row.id)">
              </el-switch>
              {{ row.isShow ? "显示" : "不显示" }}
            </template>
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="{ row }">

              <el-popconfirm title="你确定要删除吗" @confirm="deleteCurrentItem(row.id)">
                <template #reference>
                  <el-button type="danger" size="small">删除</el-button>
                </template>

              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
        <!--页码-->
        <div class="flex flex-row justify-between items-center mt-[10px]">
          <div class="text-[14px]">
            当前第{{ queryFormData.pageIndex }}页，共{{
                resultData.pageCount
            }}页，共{{ resultData.totalCount }}条
          </div>
          <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
            @current-change="currentChange" />
        </div>
      </div>
    </el-card>
    <!-- 抽屉 -->

    <el-drawer v-model="table" :append-to-body="true" size="30%">
      <el-descriptions style="box-sizing: border-box; p-[15px]" title="评论详情" :column="1" size="small" border>
        <el-descriptions-item label="菜品名称" width="50">
          {{ resultData.currentList.foodInfo.food_name }}
        </el-descriptions-item>
        <el-descriptions-item label="菜品价格">
          {{ resultData.currentList.foodInfo.price }}
        </el-descriptions-item>
        <el-descriptions-item label="评论时间">
          {{ formatDateTime(resultData.currentList.create_time) }}
        </el-descriptions-item>
        <el-descriptions-item label="客户昵称">
          {{ resultData.currentList.userInfo.nickName }}
        </el-descriptions-item>
        <el-descriptions-item label="客户性别">
          {{ resultData.currentList.userInfo.user_sex }}
        </el-descriptions-item>
        <el-descriptions-item label="客户手机号">
          {{ resultData.currentList.userInfo.user_phone }}
        </el-descriptions-item>
        <el-descriptions-item label="客户邮箱">
          {{ resultData.currentList.userInfo.user_email }}
        </el-descriptions-item>
        <el-descriptions-item label="评分等级">
          <el-rate v-model="resultData.currentList.star" disabled show-score text-color="#ff9900"
            score-template="{value}分">
          </el-rate>

        </el-descriptions-item>
        <el-descriptions-item label="评论内容">
          {{ resultData.currentList.foodInfo.food_desc }}
        </el-descriptions-item>
      </el-descriptions>

    </el-drawer>




  </page-view>
</template>

<script setup>
import { reactive, ref } from "vue";
import { Search, CirclePlus, View } from "@element-plus/icons-vue";
import { formatDateTime } from "@/utils/DateTimeUtils";
import API from "@/utils/API/index.js";
import { txtEllipsis } from "@/utils/StringUtils.js";
import { onMounted, inject } from "vue";
import { ElDrawer, ElNotification } from "element-plus";

const baseURL = inject("baseURL");
const isLoading = ref(false);

//时间选择器
const size = ref("");
const value1 = ref("");

const shortcuts = [
  {
    text: "Last week",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 7);
      return [start_time, end_time];
    },
  },
  {
    text: "Last month",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 30);
      return [start_time, end_time];
    },
  },
  {
    text: "Last 3 months",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 90);
      return [start_time, end_time];
    },
  },
];

const value = ref("");
const options = [
  {
    value: "未付款",
    label: "未付款",
  },
  {
    value: "已付款",
    label: "已付款",
  },
  {
    value: "已发货",
    label: "已发货",
  },
];

const queryFormData = reactive({
  start_time: "",
  end_time: "",
  pageIndex: 1,
  order_status: value,
  uid: "",
});

const resultData = reactive({
  listData: [],
  pageCount: 0,
  totalCount: 0,
  currentList: []
});

//查询操作
const queryData = () => {
  isLoading.value = true;
  API.commentInfo
    .getListByPage(queryFormData)
    .then((result) => {
      console.log(result);
      resultData.listData = result.listData;
      resultData.pageCount = result.pageCount;
      resultData.totalCount = result.totalCount;
    })
    .finally(() => {
      isLoading.value = false;
    });
};

//抽屉
const table = ref(false)
const DrawerMenu = (index) => {
  table.value = true;
  resultData.currentList = resultData.listData[index]
}

//是否显示操作提示
const ShowTitle = (id) => {
  console.log(id);
  ElNotification({
    title: "操作成功",
    message: "切换成功",
    type: "success",
  });
};
//页码发生变化
const currentChange = (index) => {
  queryFormData.pageIndex = index;
  queryData();
};

//删除操作
const deleteCurrentItem = (id) => {
  console.log(id);
  API.commentInfo.deleteById({ id }).then(result => {
    ElNotification({
      type: "success",
      title: "删除成功",
      message: "当前用户评论删除成功"
    })
    //重新渲染页面
    queryData()
  }).catch(error => {
    ElNotification({
      type: "danger",
      title: "删除失败",
      message: "当前用户评论删除失败，请重试或联系管理员"
    })
  })
}

onMounted(() => {
  queryData();
});
</script>

<style scoped>

</style>
